#include "gitutil.h"

#include <QProcess>

GitUtil::GitUtil() {}


QStringList GitUtil::git_remotes(const QString &dir)
{
    return run(dir, "git branch --all --remotes --no-color").split("\n", Qt::SkipEmptyParts);
}

bool GitUtil::git_status_porcelain(const QString &dir)
{
    return run(dir, "git status --porcelain").isEmpty();
}

void GitUtil::git_reset(const QString &dir, bool hard)
{
    if (hard)
        run(dir, "git reset --hard");
    else
        run(dir, "git reset");
}

bool GitUtil::is_head(const QString &dir)
{
    return run(dir, "git rev-parse --abbrev-ref HEAD").contains("HEAD");
}

QString GitUtil::run(const QString &dir, const QString &commandline)
{
    QProcess process;
    process.setWorkingDirectory(dir);
    QStringList args = commandline.split(" ", Qt::SkipEmptyParts);

    // if (merge_channel) process.setProcessChannelMode(QProcess::MergedChannels);
    process.start(args[0], args.mid(1));
    process.waitForFinished();
    QByteArray content = process.readAll();
    return content;
}
